﻿using System;

namespace Dan.Common
{
    public static class ExceptionExtend
    {
        /// <summary>
        /// 返回最底层的Exception
        /// </summary>
        /// <returns></returns>
        public static Exception GetLastInnerException(this Exception ex)
        {
            if (ex?.InnerException != null)
            {
                return GetInnerExcepiton(ex.InnerException);
            }

            return ex;
        }

        public static Exception GetInnerExcepiton(Exception ex)
        {
            if (ex != null)
            {
                var result = GetInnerExcepiton(ex.InnerException);
                if (result != null)
                {
                    return result;
                }
            }

            return ex;
        }
    }
}